package com.yikaiye.android.yikaiye.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.databinding.DialogCallBinding;
import com.yikaiye.android.yikaiye.utils.UMengUtil;

/**
 * 拨打电话
 */
public class CallDialog {
    private final Dialog dialog;
    private final DialogCallBinding binding;

    public CallDialog(Context context) {
        binding = DialogCallBinding.inflate(LayoutInflater.from(context));
        dialog = new Dialog(context, R.style.prompt_progress_dialog_dimenabled);
        dialog.setContentView(binding.getRoot(), new LinearLayout.LayoutParams
                (LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        dialog.setCancelable(true);
        Window dialogWindow = dialog.getWindow();
        if (dialogWindow != null) {
            // 去除遮罩效果
//        dialogWindow.setDimAmount(0f);
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            dialogWindow.setGravity(Gravity.BOTTOM);
            dialogWindow.setWindowAnimations(R.style.main_menu_animStyle);
            // lp.x = 266; // 新位置X坐标
            // lp.y = Utils.dip2px(activity, 300); // 新位置Y坐标
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            dialogWindow.setAttributes(lp);
        }

        binding.tvPhoneNum.setText(context.getString(R.string.tel));
        binding.tvCancel.setOnClickListener(v -> dismissDialog());
        binding.tvConfirm.setOnClickListener(view14 -> {
            Intent intent = new Intent(Intent.ACTION_CALL);
            Uri data = Uri.parse(context.getString(R.string.tel_uri, binding.tvPhoneNum.getText().toString()));
            intent.setData(data);
            context.startActivity(intent);
            dismissDialog();
            UMengUtil.setCountEventId(Constant.PAGE_DETAILS_CALL);
        });

        binding.llService1.setOnClickListener(view1 -> {
            Intent intent = new Intent(Intent.ACTION_CALL);
            Uri data = Uri.parse(context.getString(R.string.tel_uri, binding.tvPhone1.getText().toString()));
            intent.setData(data);
            context.startActivity(intent);
            dismissDialog();
        });

        binding.llService2.setOnClickListener(view12 -> {
            Intent intent = new Intent(Intent.ACTION_CALL);
            Uri data = Uri.parse(context.getString(R.string.tel_uri, binding.tvPhone2.getText().toString()));
            intent.setData(data);
            context.startActivity(intent);
            dismissDialog();
        });

        binding.ivClose.setOnClickListener(view13 -> dismissDialog());
    }

    public void setShowOne(String name, String phone) {
        // 展示一个专属客服
        binding.tvTitle1.setVisibility(View.VISIBLE);
        binding.tvTitle2.setVisibility(View.GONE);
        binding.ivClose.setVisibility(View.VISIBLE);
        binding.llExclusive.setVisibility(View.VISIBLE);
        binding.llService1.setVisibility(View.GONE);
        binding.tvPhoneNum.setVisibility(View.GONE);
        binding.llButton.setVisibility(View.GONE);
        binding.tvName2.setText(name);
        binding.tvPhone2.setText(phone);
    }

    public void setShowTwo(String name1, String phone1, String name2, String phone2) {
        // 展示两个专属客服
        binding.tvTitle1.setVisibility(View.VISIBLE);
        binding.tvTitle2.setVisibility(View.GONE);
        binding.ivClose.setVisibility(View.VISIBLE);
        binding.llExclusive.setVisibility(View.VISIBLE);
        binding.tvPhoneNum.setVisibility(View.GONE);
        binding.llButton.setVisibility(View.GONE);
        binding.tvName1.setText(name1);
        binding.tvPhone1.setText(phone1);
        binding.tvName2.setText(name2);
        binding.tvPhone2.setText(phone2);
    }

    public void showDialog() {
        dialog.show();
    }

    public void dismissDialog() {
        if (dialog != null) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }
    }
}
